<?
class WDate 
{
	private $time = 0;
	private $mode = 0;  // 本属性为与JavaScript兼容而设，$mode=1为JavaScript方式
	private $datemode = "Y-m-d H:i:s";  // 日期格式 "Y-m-d H:i:s"，可自行设定
	function WDate($t=0) 
	{
		if($t == 0)
			$this->time = time();
	}
	/**
	* 返回从GMT时间1970年1月1日0时开始的毫秒数
	*/
	function getTime() 
	{
		$temp = gettimeofday();
		return $temp['sec']*1000+round($temp['usec']/1000);
	}
	/**
	* 返回年份
	*/
	function getYear() 
	{
		$temp = getdate($this->time);
		return $temp['year'];
	}
	/**
	* 返回月份
	*/
	function getMonth() 
	{
		$temp = getdate($this->time);
		return $temp['mon']-$this->mode;
	}
	/**
	* 返回日期
	*/
	function getDate() 
	{
		$temp = getdate($this->time);
		return $temp['mday'];
	}
	/**
	* 返回星期
	*/
	function getDay() 
	{
		$temp = getdate($this->time);
		return $temp['wday']-$this->mode;
	}
	/**
	* 返回小时
	*/
	function getHours() 
	{
		$temp = getdate($this->time);
		return $temp['hours'];
	}
	/**
	* 返回分
	*/
	function getMinutes() 
	{
		$temp = getdate($this->time);
		return $temp['minutes'];
	}
	/**
	* 返回秒
	*/
	function getSeconds() 
	{
		$temp = getdate($this->time);
		return $temp['seconds'];
	}
	/**
	* 设定年份
	* php 4.0.6 year 1970 -- 2038
	*/
	function setYear($val) 
	{
		$temp = getdate($this->time);
		$temp['year'] = $val;
		$this->set_time($temp);
	}
	/**
	* 设定月份
	*/
	function setMonth($val) 
	{
		$temp = getdate($this->time);
		$temp['mon'] = $val+$this->mode;
		$this->set_time($temp);
	}
	/**
	* 设定日期
	*/
	function setDate($val) 
	{
		$temp = getdate($this->time);
		$temp['mday'] = $val;
		$this->set_time($temp);
	}
	/**
	* 设定星期
	*/
	function setDay($val) 
	{
		$temp = getdate($this->time);
		$temp['wday'] = $val+$this->mode;
		$this->set_time($temp);
	}
	/**
	* 设定小时
	*/
	function setHours($val) 
	{
		$temp = getdate($this->time);
		$temp['hours'] = $val;
		$this->set_time($temp);
	}
	/**
	* 设定分
	*/
	function setMinutes($val) 
	{
		$temp = getdate($this->time);
		$temp['minutes'] = $val;
		$this->set_time($temp);
	}
	/**
	* 设定秒
	*/
	function setSeconds($val) 
	{
		$temp = getdate($this->time);
		$temp['seconds'] = $val;
		$this->set_time($temp);
	}
	/**
	* 返回系统格式的字符串
	*/
	function toLocaleString() 
	{
		return date($this->datemode,$this->time);
	}
	/**
	* 使用GTM时间创建一个日期值
	*/
	function UTC($year,$mon,$mday,$hours=0,$minutes=0,$seconds=0) 
	{
		$this->time = mktime($hours,$minutes,$seconds,$mon,$mday,$year);
		return $this->time;
	}
	/**
	* 等价于DateAdd(interval,number,date)
	* 返回已添加指定时间间隔的日期。
	* Inetrval为表示要添加的时间间隔字符串表达式，例如分或天
	* number为表示要添加的时间间隔的个数的数值表达式
	* Date表示日期
	*
	* Interval(时间间隔字符串表达式)可以是以下任意值: 
	*  yyyy year年 
	*  q Quarter季度 
	*  m Month月 
	*  y Day of year一年的数 
	*  d Day天 
	*  w Weekday一周的天数 
	*  ww Week of year周 
	*  h Hour小时 
	*  n Minute分 
	*  s Second秒 
	*  w、y和d的作用是完全一样的，即在目前的日期上加一天，q加3个月，ww加7天。 
	*/
	function Add($interval, $number, $date) 
	{
		$date = $this->get_time($date);
		$date_time_array = getdate($date); 
		$hours = $date_time_array["hours"]; 
		$minutes = $date_time_array["minutes"]; 
		$seconds = $date_time_array["seconds"]; 
		$month = $date_time_array["mon"]; 
		$day = $date_time_array["mday"]; 
		$year = $date_time_array["year"]; 
		switch ($interval) 
		{ 
			case "yyyy": $year +=$number; break; 
			case "q": $month +=($number*3); break; 
			case "m": $month +=$number; break; 
			case "y": 
			case "d": 
			case "w": $day+=$number; break; 
			case "ww": $day+=($number*7); break; 
			case "h": $hours+=$number; break; 
			case "n": $minutes+=$number; break; 
			case "s": $seconds+=$number; break; 
		} 
		$temptime = mktime($hours ,$minutes, $seconds,$month ,$day, $year); 
		return $temptime;
	} 
	/**
	* 等价于DateDiff(interval,date1,date2)
	* 返回两个日期之间的时间间隔
	* intervals(时间间隔字符串表达式)可以是以下任意值: 
	*   w  周
	*   d  天
	*   h  小时
	*   n  分钟
	*   s  秒
	*/
	function Diff($interval, $date1,$date2) 
	{ 
		// 得到两日期之间间隔的秒数 
		$timedifference = $this->get_time($date2) - $this->get_time($date1); 
		switch ($interval) 
		{ 
			case "w": $retval = $this->bcdiv($timedifference ,604800); break; 
			case "d": $retval = $this->bcdiv( $timedifference,86400); break; 
			case "h": $retval = $this->bcdiv ($timedifference,3600); break; 
			case "n": $retval = $this->bcdiv( $timedifference,60); break; 
			case "s": $retval = $timedifference; break; 
		} 
		return $retval;
	} 
	function bcdiv($a,$b,$scale=0)
	{
		$v = $a/$b;
		$v = sprintf("%.{$scale}f", $v);
		return "$v";
	}
	/**
	* 输出，根据需要直接修改本函数或在派生类中重写本函数
	*/
	function display() 
	{
		$nStr = array('日','一','二','三','四','五','六');
		echo sprintf("%4d年%2d月%2d日 星期%s<br>",$this->getYear(),$this->getMonth(),$this->getDate(),$nStr[$this->getDay()%7]);
	}
	/**
	* 工作函数
	*/
	function set_time(&$ar) 
	{
		$this->time = mktime($ar['hours'],$ar['minutes'],$ar['seconds'],$ar['mon'],$ar['mday'],$ar['year']);
	}
	/**
	* 转换为UNIX时间戳
	*/
	function get_time($d) 
	{
		if(is_numeric($d))
			return $d;
		else 
		{
			if(! is_string($d)) 
				return 0;
			if(ereg(":",$d)) 
			{
				$buf = split(" +",$d);
				$year = split("[-/]",$buf[0]);
				$hour = split(":",$buf[1]);
				if(isset($buf[2]) && eregi("pm",$buf[2]))
					$hour[0] += 12;
				return mktime($hour[0],$hour[1],$hour[2],$year[1],$year[2],$year[0]);
			}else 
			{
				$year = split("[-/]",$d);
				return mktime(0,0,0,$year[1],$year[2],$year[0]);
			}
		}
	}
}
?>